//
//  CorgiEmoticonPageViewCell.swift
//  weibo
//
//  Created by Corgi on 17/3/30.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

class CorgiEmoticonPageViewCell: UICollectionViewCell {
 
    var emoticons: [CorgiEmoticonModel]? {
    
        didSet {
            
            for button in emoticonButtons {
            
                button.isHidden = true
            }
            
            for (i, emoticonModel) in emoticons!.enumerated() {
            
                let button = emoticonButtons[i]
                
                button.emoticonModel = emoticonModel
            
            }
        }
    }
    
    var indexPath: IndexPath? {
        
        didSet {
        
            messageLabel.text = "Section:\(indexPath!.section)\nItem:\(indexPath!.item)"
        }
    }
 
    var emoticonButtons: [CorgiEmoticonButton] = [CorgiEmoticonButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        let buttonWidth: CGFloat = (screenWidth - 10) / CGFloat(CORGIEMOTICONCOL)
        
        let buttonHeight: CGFloat = (216 - 35 - 20) / CGFloat(CORGIEMOTICONROW)
        
        for (i, button) in emoticonButtons.enumerated() {
        
            let column = CGFloat(i % CORGIEMOTICONCOL)
            
            let row = CGFloat(i / CORGIEMOTICONCOL)
            
            button.frame = CGRect(x: column * buttonWidth + 5, y: row * buttonHeight, width: buttonWidth, height: buttonHeight)
        }
        
        deleteButton.frame = CGRect(x: screenWidth - buttonWidth - 5, y: buttonHeight * 2, width: buttonWidth, height: buttonHeight)
    }
    
    fileprivate func setupUI() {
        
//        backgroundColor = randomColor()
        
        addChildButtons()
        
        contentView.addSubview(messageLabel)
        
        contentView.addSubview(deleteButton)
        
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    
    fileprivate lazy var messageLabel: UILabel = {
    
        let label = UILabel(title: "", textColor: UIColor.red, fontSize: 20)
        
        return label
    }()
    
    fileprivate lazy var deleteButton: UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: #selector(deleteButtonClick), for: .touchUpInside)
        
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        
        return button
    }()
}

extension CorgiEmoticonPageViewCell {

    fileprivate func addChildButtons() {
        
        for _ in 0..<CORGIEMOTICONCOUNT {
     
            let button = CorgiEmoticonButton()
            
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        
            button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
            
//            button.backgroundColor = randomColor()
            
            emoticonButtons.append(button)
        
            contentView.addSubview(button)
        }
    }
}

extension CorgiEmoticonPageViewCell {

    @objc fileprivate func buttonClick(button: CorgiEmoticonButton) {
     
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: EMOTICONBUTTONCLICKNOTI), object: button.emoticonModel)
    }
    
    @objc fileprivate func deleteButtonClick() {
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: EMOTICONDELETEBUTTONCLICK), object: nil)
        
    }
}
